package com.dyh.quickdemozwei.pipe0428;

public class PipeUnitSelfAdapt<T> {

    PipeUnit<T> pipeUnit;

    Class<T> inputClass;

    private PipeUnitSelfAdapt(PipeUnit<T> pipeUnit, Class<T> inputClass) {
        this.pipeUnit = pipeUnit;
        this.inputClass = inputClass;
    }

    public static <T> PipeUnitSelfAdapt<T> from(PipeUnit<T> pipeUnit, Class<T> inputClass){
        return new PipeUnitSelfAdapt<>(pipeUnit,inputClass);
    }

    Object workSelfAdapt(Object obj){
        if (obj==null)
           return null;
        if (inputClass.isAssignableFrom(obj.getClass())) {
           return pipeUnit.work((T)obj);
        }
        return null;
    }


}
